#!/usr/bin/env python
"""

This script does a quick XML validation of all the files in ~/.shelllogger.

It returns nothing if all XML files found validate. 

"""
import os
from optparse import OptionParser
from xml.parsers.expat import ExpatError
import xml.etree.ElementTree as ET 


def check_file(filename):
    try:
        ET.parse(filename)
        print "%s valid" % filename
    except ExpatError, e:
        print filename, 
        print e

def check_directory(dirname):
    print "Directory:", dirname
    for fname in os.listdir(dirname):
         if fname.endswith('.xml'):
            check_file(os.path.join(dirname,fname))
            
if __name__ == '__main__':
    usage = "usage: %prog [FILE.xml|DIR]"
    description = "Checks that FILE.xml or files with .xml extension in DIR are valid XML"
    epilog = "If no directory is specified, will use DIR=~/.shelllogger"
    parser = OptionParser(usage=usage, description=description, epilog=epilog)
    (options, args) = parser.parse_args()
    if len(args)>1:
        parser.error("incorrect number of arguments")

    if len(args)==0:
        check_directory(os.path.expanduser('~/.shelllogger'))
    else:
        pathname = args[0]
        if os.path.isfile(pathname):
            check_file(pathname)
        elif os.path.isdir(pathname):
            check_directory(pathname)
        else:
            print "Path not found: %s" % pathname

